package com.bigstar.lolen_e;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class Item extends Activity implements OnClickListener{
	
	private int select_item;
	
    @Override
    protected void onResume() {
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
    	super.onResume();
    }
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
		setContentView(R.layout.item);
        AdView adview = (AdView)findViewById(R.id.adView);
        AdRequest re = new AdRequest(); 
        adview.loadAd(re);
		select_item = Integer.parseInt(getIntent().getExtras().getString("itemid"));
		Button button = null;
		for(int i = 0; i < ItemData.builds_item[select_item].length; i++){
			button = (Button)findViewById(R.id.builds_item_0+i);
			button.setBackgroundResource(R.drawable.item_000+ItemData.builds_item[select_item][i]);
			button.setOnClickListener(this);
		}
		for(int i = ItemData.builds_item[select_item].length; i < 6; i++){
			button = (Button)findViewById(R.id.builds_item_0+i);
			button.setVisibility(View.INVISIBLE);
		}
		TextView label = (TextView)findViewById(R.id.item_info_name);
		String text = ItemData.items.get(select_item).name_en + " "+
        "("+ItemData.items.get(select_item).price+"Gold)";
		label.setText(text);
		text = "";
		if(ItemData.items.get(select_item).text != null){
			text =ItemData.items.get(select_item).text;
			if(ItemData.items.get(select_item).unique!=null){
				text = text + "<br>";
			}
		}
		if(ItemData.items.get(select_item).unique!=null){
			text = text + ItemData.items.get(select_item).unique;
			text = text.replaceAll(":br:", "<br>");
		}
		label = (TextView)findViewById(R.id.item_info_text);
		label.setText(Html.fromHtml(text));
		
		ImageView icon = (ImageView)findViewById(R.id.item_info_img);
	       
		icon.setImageResource(R.drawable.item_000+select_item);
		
		
		icon = (ImageView)findViewById(R.id.item_requires_img);
		icon.setImageResource(R.drawable.item_000+select_item);
		int last_price = ItemData.items.get(select_item).price;
		int price = 0;
	    if(ItemData.requires_item[select_item].length == 1){
	    	LinearLayout layout = (LinearLayout)findViewById(R.id.item_material_0_layout);
	    	layout.setVisibility(View.VISIBLE);
			icon = (ImageView)findViewById(R.id.item_material_0_img);
			icon.setImageResource(R.drawable.item_000+ItemData.requires_item[select_item][0]);
			price = ItemData.items.get(ItemData.requires_item[select_item][0]).price;
			label = (TextView)findViewById(R.id.item_material_0_price);
			label.setText(price+"Gold");
			last_price -= price;
			icon.setOnClickListener(this);
	    }else if(ItemData.requires_item[select_item].length == 2){
	    	LinearLayout layout = (LinearLayout)findViewById(R.id.item_material_0_layout);
	    	layout.setVisibility(View.VISIBLE);
	    	layout = (LinearLayout)findViewById(R.id.item_material_1_layout);
	    	layout.setVisibility(View.VISIBLE);
			icon = (ImageView)findViewById(R.id.item_material_0_img);
			icon.setImageResource(R.drawable.item_000+ItemData.requires_item[select_item][0]);
			price = ItemData.items.get(ItemData.requires_item[select_item][0]).price;
			label = (TextView)findViewById(R.id.item_material_0_price);
			label.setText(price+"Gold");
			last_price -= price;
			icon.setOnClickListener(this);
			
			icon = (ImageView)findViewById(R.id.item_material_1_img);
			icon.setImageResource(R.drawable.item_000+ItemData.requires_item[select_item][1]);
			price = ItemData.items.get(ItemData.requires_item[select_item][1]).price;
			label = (TextView)findViewById(R.id.item_material_1_price);
			label.setText(price+"Gold");
			last_price -= price;
			icon.setOnClickListener(this);
	    }else if(ItemData.requires_item[select_item].length == 3){
	    	LinearLayout layout = (LinearLayout)findViewById(R.id.item_material_0_layout);
	    	layout.setVisibility(View.VISIBLE);
	    	layout = (LinearLayout)findViewById(R.id.item_material_1_layout);
	    	layout.setVisibility(View.VISIBLE);
	    	layout = (LinearLayout)findViewById(R.id.item_material_2_layout);
	    	layout.setVisibility(View.VISIBLE);
			icon = (ImageView)findViewById(R.id.item_material_0_img);
			icon.setImageResource(R.drawable.item_000+ItemData.requires_item[select_item][0]);
			price = ItemData.items.get(ItemData.requires_item[select_item][0]).price;
			label = (TextView)findViewById(R.id.item_material_0_price);
			label.setText(price+"Gold");
			last_price -= price;
			icon.setOnClickListener(this);
			icon = (ImageView)findViewById(R.id.item_material_1_img);
			icon.setImageResource(R.drawable.item_000+ItemData.requires_item[select_item][1]);
			price = ItemData.items.get(ItemData.requires_item[select_item][1]).price;
			label = (TextView)findViewById(R.id.item_material_1_price);
			label.setText(price+"Gold");
			last_price -= price;
			icon.setOnClickListener(this);
			icon = (ImageView)findViewById(R.id.item_material_2_img);
			icon.setImageResource(R.drawable.item_000+ItemData.requires_item[select_item][2]);
			price = ItemData.items.get(ItemData.requires_item[select_item][2]).price;
			label = (TextView)findViewById(R.id.item_material_2_price);
			label.setText(price+"Gold");
			last_price -= price;
			icon.setOnClickListener(this);
	    }else if(ItemData.requires_item[select_item].length == 4){
	    	LinearLayout layout = (LinearLayout)findViewById(R.id.item_material_0_layout);
	    	layout.setVisibility(View.VISIBLE);
	    	layout = (LinearLayout)findViewById(R.id.item_material_1_layout);
	    	layout.setVisibility(View.VISIBLE);
	    	layout = (LinearLayout)findViewById(R.id.item_material_2_layout);
	    	layout.setVisibility(View.VISIBLE);
	    	layout = (LinearLayout)findViewById(R.id.item_material_3_layout);
	    	layout.setVisibility(View.VISIBLE);
			icon = (ImageView)findViewById(R.id.item_material_0_img);
			icon.setImageResource(R.drawable.item_000+ItemData.requires_item[select_item][0]);
			price = ItemData.items.get(ItemData.requires_item[select_item][0]).price;
			label = (TextView)findViewById(R.id.item_material_0_price);
			label.setText(price+"Gold");
			last_price -= price;
			icon.setOnClickListener(this);
			icon = (ImageView)findViewById(R.id.item_material_1_img);
			icon.setImageResource(R.drawable.item_000+ItemData.requires_item[select_item][1]);
			price = ItemData.items.get(ItemData.requires_item[select_item][1]).price;;
			label = (TextView)findViewById(R.id.item_material_1_price);
			label.setText(price+"Gold");
			last_price -= price;
			icon.setOnClickListener(this);
			icon = (ImageView)findViewById(R.id.item_material_2_img);
			icon.setImageResource(R.drawable.item_000+ItemData.requires_item[select_item][2]);
			price = ItemData.items.get(ItemData.requires_item[select_item][2]).price;
			label = (TextView)findViewById(R.id.item_material_2_price);
			label.setText(price+"Gold");
			last_price -= price;
			icon.setOnClickListener(this);
			icon = (ImageView)findViewById(R.id.item_material_3_img);
			icon.setImageResource(R.drawable.item_000+ItemData.requires_item[select_item][3]);
			price = ItemData.items.get(ItemData.requires_item[select_item][3]).price;
			label = (TextView)findViewById(R.id.item_material_3_price);
			label.setText(price+"Gold");
			last_price -= price;
			icon.setOnClickListener(this);
	    }
	    
		label = (TextView)findViewById(R.id.item_requires_price);
		label.setText(last_price+"Gold");
    }
    
	@Override
	public void onClick(View v) {
	    if(ItemData.requires_item[select_item].length == 1){
	    	if(v.getId() == R.id.item_material_0_img){
	    		Intent intent = new Intent(this, Item.class);
	    		intent.putExtra("itemid", ""+ItemData.requires_item[select_item][0]);
	    		startActivity(intent);
	    		return;
	    	}
	    }else if(ItemData.requires_item[select_item].length == 2){
	    	if(v.getId() == R.id.item_material_0_img){
	    		Intent intent = new Intent(this, Item.class);
	    		intent.putExtra("itemid", ""+ItemData.requires_item[select_item][0]);
	    		startActivity(intent);
	    		return;
	    	}else if(v.getId() == R.id.item_material_1_img){
	    		Intent intent = new Intent(this, Item.class);
	    		intent.putExtra("itemid", ""+ItemData.requires_item[select_item][1]);
	    		startActivity(intent);
	    		return;
	    	}
	    }else if(ItemData.requires_item[select_item].length == 3){
	    	if(v.getId() == R.id.item_material_0_img){
	    		Intent intent = new Intent(this, Item.class);
	    		intent.putExtra("itemid", ""+ItemData.requires_item[select_item][0]);
	    		startActivity(intent);
	    		return;
	    	}else if(v.getId() == R.id.item_material_1_img){
	    		Intent intent = new Intent(this, Item.class);
	    		intent.putExtra("itemid", ""+ItemData.requires_item[select_item][1]);
	    		startActivity(intent);
	    		return;
	    	}else if(v.getId() == R.id.item_material_2_img){
	    		Intent intent = new Intent(this, Item.class);
	    		intent.putExtra("itemid", ""+ItemData.requires_item[select_item][2]);
	    		startActivity(intent);
	    		return;
	    	}
	    }else if(ItemData.requires_item[select_item].length == 4){
	    	if(v.getId() == R.id.item_material_0_img){
	    		Intent intent = new Intent(this, Item.class);
	    		intent.putExtra("itemid", ""+ItemData.requires_item[select_item][0]);
	    		startActivity(intent);
	    		return;
	    	}else if(v.getId() == R.id.item_material_1_img){
	    		Intent intent = new Intent(this, Item.class);
	    		intent.putExtra("itemid", ""+ItemData.requires_item[select_item][1]);
	    		startActivity(intent);
	    		return;
	    	}else if(v.getId() == R.id.item_material_2_img){
	    		Intent intent = new Intent(this, Item.class);
	    		intent.putExtra("itemid", ""+ItemData.requires_item[select_item][2]);
	    		startActivity(intent);
	    		return;
	    	}else if(v.getId() == R.id.item_material_3_img){
	    		Intent intent = new Intent(this, Item.class);
	    		intent.putExtra("itemid", ""+ItemData.requires_item[select_item][3]);
	    		startActivity(intent);
	    		return;
	    	}
	    }
		Intent intent = new Intent(this, Item.class);
		intent.putExtra("itemid", ""+ItemData.builds_item[select_item][v.getId()-R.id.builds_item_0]);
		startActivity(intent);
	}
}
